package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    /**
     * 攻略详情页面
     * @param id 攻略id
     * @return 攻略
     */
    @GetMapping("/detail")
    public Object detail(String id) {

        Strategy strategy = strategyService.get(id);

        // 阅读数+1 不能在get里面+1
        strategyStatisVORedisService.viewnumIncrease(id, 1);


        return JsonResult.success(strategy);
    }

    /**
     * 攻略列表 分页查询
     * @param qo 查询条件对象 包含 目的地id 攻略主题id
     * @return 攻略的集合 符合条件的
     */
    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        qo.setState("1");
        return JsonResult.success(strategyService.query(qo));
    }

    /**
     * 攻略主题集合
     * @return 攻略主题集合
     */
    @GetMapping("/themes")
    public Object themes() {

        return JsonResult.success(strategyThemeService.list());
    }

    /**
     * 添加评论
     * 需要登录
     * @param comment
     * @param userInfo
     * @return
     */
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo) {

        // 设置用户信息 id名字不一样 所以需要手动赋值
        comment.setUserId(userInfo.getId());
        // BeanUtils 是bean的一个工具类
        // copyProperties 用于bean属性拷贝 属性名字必须一致才能拷贝
        // 这个拷贝会覆盖 domain父类的id 这个不可以 但是在save中 已经将id设置为null了
        BeanUtils.copyProperties(userInfo, comment);
        strategyCommentService.save(comment);

        // 评论数+1
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(), 1);

        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo) {

        return JsonResult.success(strategyCommentService.query(qo));
    }

    @RequireLogin
    @PostMapping("/commentThumb")
    public Object comments(String cid, @UserParam UserInfo userInfo) {

        strategyCommentService.commentThumb(cid, userInfo.getId());

        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }


    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo) {

        boolean isFavor = strategyStatisVORedisService.favor(sid, userInfo.getId());

        return JsonResult.success(isFavor);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {

        boolean isFavor = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());

        return JsonResult.success(isFavor);
    }

}





































